package com.an0nymax.control;

import java.util.ArrayList;

import com.wpg.proxy.HttpMessageRequest;
import com.wpg.proxy.HttpMessageResponse;

public class HtmlParser {
	public ArrayList<LinkInfo> aryLink = new ArrayList<LinkInfo>();
	public String oriStr;
	
	public HtmlParser(String str) {
		oriStr = str;
		parseLink();
	}
	
	public HtmlParser(HttpMessageRequest request, HttpMessageResponse response) {
		parseLink(request, response);
	}
	
	public void parseLink() {
		int start = 0;
		
//		start = oriStr.indexOf("<a href=\"board_view.php");
		start = oriStr.indexOf("<a href=\"http://news.naver.com/main/read.nhn");
		while (start > 0) {
			int end = oriStr.indexOf("\">", start);
			LinkInfo link = new LinkInfo();
			link.setUrl(oriStr.substring(start - 9, end));
			
			start = end;
			end = oriStr.indexOf("</a>", start);
			link.setTitle(oriStr.substring(start + 2, end));
			
			start = oriStr.indexOf("brd_smr>", end);
			end = oriStr.indexOf("</td>", start);
			link.setTitle(oriStr.substring(start + 8, end));
			
			aryLink.add(link);
			start = end;
		}
	}
	
	public void parseLink(HttpMessageRequest request, HttpMessageResponse response) {
		if (response.getStatusCode() == 200) {
			if (response.getHeaderValues("content-type") != null
					&& response.getHeaderValues("content-type").get(0).startsWith("text/html")) {
				System.out.println("text/html");
			}
			
		}
	}
}
